package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.CartItem;
import isp.apps.example.shoppingcart.domain.Item;
import isp.apps.example.shoppingcart.persistence.CartItemsDirectory;
import isp.apps.example.shoppingcart.persistence.factory.CartItemsPersistenceFactory;

import isp.apps.example.shoppingcart.persistence.ItemDirectory;

import isp.apps.example.shoppingcart.persistence.factory.ItemPersistenceFactory;

import java.math.BigDecimal;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SalesByItemAction extends ActionSupport
{
  private Integer totalOrders; 
  private Integer totalVolume;
  private BigDecimal averageVolume = new BigDecimal(0);
  private BigDecimal netSales = new BigDecimal(0); 
  private BigDecimal averagePrice = new BigDecimal(0);
  private CartItemsDirectory myCartDir = CartItemsPersistenceFactory
    .getCartItemsDirectory();
  private ItemDirectory myItemDir = ItemPersistenceFactory
    .getItemDirectory();
  private Integer selectedItem;
  private Item item;
  private List cartItems;
 
  public SalesByItemAction()
  {
  }

  
  public String execute()
  {
  validate();
  if (hasActionErrors())
     return INPUT;
  cartItems = myCartDir.getCartItemsByItemId(selectedItem);
  item = myItemDir.getItem(selectedItem);
  doStats();
  return SUCCESS;
  }
  
  public void doStats()
  {
    int totalVolumePrimitive=0;
    totalOrders= cartItems.size();
    if (totalOrders==0)
    {
       totalVolume = new Integer(0);
       return;
    }
    for (Iterator i = cartItems.iterator(); i.hasNext();)
    {
      CartItem ci = (CartItem)i.next();
      totalVolumePrimitive += ci.getQuantity();
      netSales = netSales.add(ci.getCost().multiply(new BigDecimal(ci.getQuantity())));
    }
    totalVolume = new Integer(totalVolumePrimitive);
    averagePrice.setScale(2);
    averageVolume.setScale(2);
    averagePrice = netSales.divide(new BigDecimal(totalVolume),BigDecimal.ROUND_HALF_UP);
    averageVolume = new BigDecimal(totalVolumePrimitive).setScale(2);
    averageVolume = averageVolume.divide(new BigDecimal(totalOrders),BigDecimal.ROUND_HALF_UP);
  }
  
  public void validate()
  {
  }

  public void setSelectedItem(Integer selectedItem)
  {
    this.selectedItem = selectedItem;
  }

  public Integer getSelectedItem()
  {
    return selectedItem;
  }
  
  public Item getItem()
  {
     return item;
  }
  
  public List getCartItems()
  {
  return cartItems;
  }

  public Integer getTotalOrders()
  {
    return totalOrders;
  }

  public Integer getTotalVolume()
  {
    return totalVolume;
  }

  public BigDecimal getAverageVolume()
  {
    return averageVolume;
  }

  public BigDecimal getNetSales()
  {
    return netSales;
  }

  public BigDecimal getAveragePrice()
  {
    return averagePrice;
  }

  public void setItem(Item item)
  {
    this.item = item;
  }

  public void setCartItems(List cartItems)
  {
    this.cartItems = cartItems;
  }
}
